﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Engine.GameClass
{
    
    class CharacterCollection<T>:System.Collections.ArrayList
    {
        public delegate void HandlerCharacter(object sender,CharacterEvent<T> e);
        public event HandlerCharacter CharacterPreadded;
        public event HandlerCharacter CharacterAdded;
        public event HandlerCharacter CharacterPreremoved;
        public event HandlerCharacter CharacterRemoved;
        public CharacterCollection():base()
        {

        }
        public CharacterCollection(int capacity)
            : base(capacity)
        {
            
        }
        public override int Add(object value)
        {
            preadd((T)value);
            int add=base.Add(value);
            afteradd((T)value);
            return add;
        }
        public override void AddRange(System.Collections.ICollection c)
        {
            //
            //base.AddRange(c);
        }
        public override void Remove(object obj)
        {
            preremove((T)obj);
            base.Remove(obj);
            afterremove((T)obj);
        }
        public T this[int index]
        {
            get
            {
                return (T)base[index];
            }
            set
            {
                base[index] = value;
            }
        }
        void afterremove(T value)
        {
            if (CharacterRemoved != null)
            {
                CharacterEvent<T> e = new CharacterEvent<T>() { element = value };
                CharacterRemoved(this, e);
            }
        }
        void preremove(T value)
        {
            if (CharacterPreremoved != null)
            {
                CharacterEvent<T> e = new CharacterEvent<T>() { element = value };
                CharacterPreremoved(this, e);
            }
        }
        void afteradd(T value)
        {
            if (CharacterAdded != null)
            {
                CharacterEvent<T> e = new CharacterEvent<T>() { element = value };
                CharacterAdded(this, e);
            }
        }
        void preadd(T value)
        {
            if (CharacterPreadded != null)
            {
                CharacterEvent<T> e = new CharacterEvent<T>() { element = value };
                CharacterPreadded(this, e);
            }
        }

        
    }
   public class Characters
    {
       NOWAEngine _game;
       ICharacter _player;
       CharacterCollection<ICharacter> _players = new CharacterCollection<ICharacter>(0);
       CharacterCollection<ICharacter> _NPC = new CharacterCollection<ICharacter>(0);
      
       public Characters(NOWAEngine game)
       {
           _game = game;
           _players.CharacterAdded += new CharacterCollection<ICharacter>.HandlerCharacter(_players_CharacterAdded);
           _players.CharacterRemoved += new CharacterCollection<ICharacter>.HandlerCharacter(_players_CharacterRemoved);
           _NPC.CharacterAdded += new CharacterCollection<ICharacter>.HandlerCharacter(_players_CharacterAdded);
           _NPC.CharacterRemoved += new CharacterCollection<ICharacter>.HandlerCharacter(_players_CharacterRemoved);
           
       }

       public bool addPlayer(ICharacter player)
       {
           if (_player!=null)
           {
               return false;
           }
           else
           {
               _player = player;
               return true;
           }
       }

       public void addPlayers(ICharacter pl)
       {
           if (!_players.Contains(pl))
	{
		 _players.Add(pl);
	}
       }
       public void removePlayers(ICharacter pl)
       {
           if (_players.Contains(pl))
           {
               _players.Remove(pl);
           }
       }

       void _players_CharacterRemoved(object sender, CharacterEvent<ICharacter> e)
       {
           for (int i = 0; i < _players.Count; i++)
           {
               _players[i].ID = i;
           }
       }

       void _players_CharacterAdded(object sender, CharacterEvent<ICharacter> e)
       {
           e.element.ID = _players.Count - 1;
       }



       internal void Update(Microsoft.Xna.Framework.GameTime gameTime)
       {
          
           _player.Update(gameTime);
           chekInit();
           for (int i = 0; i < _players.Count; i++)
           {
               _players[i].Update(gameTime);
           }
           for (int i = 0; i < _NPC.Count; i++)
           {
               _NPC[i].Update(gameTime);
           }
       }
       void chekInit()
       {
           for (int i = 0; i < _players.Count; i++)
           {
               if (!_players[i].Init)
               {
                     _players[i].Initialize();
               }
             
           }
           for (int i = 0; i < _NPC.Count; i++)
           {
               if (!_NPC[i].Init)
               {
                   _NPC[i].Initialize();
               }
             
           }
       }
       internal void Initialize()
       {
           _player.Initialize();
           for (int i = 0; i < _players.Count; i++)
           {
               _players[i].Initialize();
           }
           for (int i = 0; i < _NPC.Count; i++)
           {
               _NPC[i].Initialize();
           }
       }
    }
}
